<template>
  <div class="register-container">
    <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">非遗文化用户注册</h3>
      <el-form-item prop="username">
        <el-input
          v-model="registerForm.username"
          type="text"
          size="large"
          placeholder="请输入账号"
        >
          <template #prefix>
            <el-icon><user /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="nickName">
        <el-input
          v-model="registerForm.nickName"
          type="text"
          size="large"
          placeholder="请输入昵称"
        >
          <template #prefix>
            <el-icon><user-filled /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          size="large"
          placeholder="请输入密码"
          @keyup.enter="handleRegister"
        >
          <template #prefix>
            <el-icon><lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          size="large"
          placeholder="请确认密码"
          @keyup.enter="handleRegister"
        >
          <template #prefix>
            <el-icon><lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="phonenumber">
        <el-input
          v-model="registerForm.phonenumber"
          type="text"
          size="large"
          placeholder="请输入手机号码"
        >
          <template #prefix>
            <el-icon><phone /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input
          v-model="registerForm.email"
          type="text"
          size="large"
          placeholder="请输入邮箱"
        >
          <template #prefix>
            <el-icon><message /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="registerForm.code"
          size="large"
          placeholder="请输入验证码"
          style="width: 63%"
          @keyup.enter="handleRegister"
        >
          <template #prefix>
            <el-icon><picture /></el-icon>
          </template>
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/user/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, UserFilled, Lock, Phone, Message, Picture } from '@element-plus/icons-vue'
import { getCodeImg, register } from '@/api/user/auth'

const router = useRouter()

const registerFormRef = ref(null)
const registerForm = ref({
  username: '',
  nickName: '',
  password: '',
  confirmPassword: '',
  phonenumber: '',
  email: '',
  code: '',
  uuid: ''
})

const registerRules = {
  username: [
    { required: true, trigger: 'blur', message: '请输入您的账号' },
    { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
  ],
  nickName: [
    { required: true, trigger: 'blur', message: '请输入您的昵称' }
  ],
  password: [
    { required: true, trigger: 'blur', message: '请输入您的密码' },
    { min: 6, max: 20, message: '用户密码长度必须介于 6 和 20 之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, trigger: 'blur', message: '请再次输入密码' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  phonenumber: [
    { required: true, trigger: 'blur', message: '请输入手机号码' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  email: [
    { required: true, trigger: 'blur', message: '请输入邮箱' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  code: [
    { required: true, trigger: 'blur', message: '请输入验证码' }
  ]
}

const codeUrl = ref('')
const loading = ref(false)
const captchaEnabled = ref(true)

function handleRegister() {
  registerFormRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      register(registerForm.value).then(res => {
        if (res.code === 200) {
          ElMessage.success('注册成功')
          router.push('/user/login')
        } else {
          ElMessage.error(res.msg)
          if (captchaEnabled.value) {
            getCode()
          }
        }
      }).catch(() => {
        if (captchaEnabled.value) {
          getCode()
        }
      }).finally(() => {
        loading.value = false
      })
    }
  })
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      registerForm.value.uuid = res.uuid
    }
  })
}

onMounted(() => {
  getCode()
})
</script>

<style lang="scss" scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.2)), url("@/assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    backdrop-filter: blur(2px);
    z-index: 0;
  }
}

.register-form {
  position: relative;
  z-index: 1;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.98);
  width: 380px;
  padding: 30px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  }

  .title {
    text-align: center;
    margin: 0 0 30px;
    font-size: 24px;
    color: #2c3e50;
    font-weight: 600;
    letter-spacing: 1px;
    position: relative;
    padding-bottom: 15px;

    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 50px;
      height: 3px;
      background: linear-gradient(90deg, #409EFF, #79bbff);
      border-radius: 3px;
    }
  }

  .el-form-item {
    margin-bottom: 20px;
  }

  .el-input {
    height: 42px;

    :deep(input) {
      height: 42px;
      padding-left: 42px;
      background: #f8f9fa;
      border: 2px solid transparent;
      border-radius: 10px;
      font-size: 14px;
      color: #2c3e50;
      transition: all 0.3s ease;

      &::placeholder {
        color: #a8adb3;
      }

      &:hover {
        background: #f0f2f5;
      }

      &:focus {
        background: #fff;
        border-color: #409EFF;
        box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
      }
    }

    :deep(.el-input__prefix) {
      left: 12px;
      font-size: 16px;
      color: #a8adb3;
      transition: all 0.3s ease;
    }

    &:hover {
      :deep(.el-input__prefix) {
        color: #409EFF;
      }
    }
  }

  .register-code {
    width: 35%;
    height: 42px;
    float: right;
    margin-left: 8px;

    img {
      width: 100%;
      height: 100%;
      cursor: pointer;
      border-radius: 10px;
      transition: all 0.3s ease;
      border: 1px solid #e4e7ed;

      &:hover {
        opacity: 0.8;
        transform: scale(1.02);
      }
    }
  }

  .el-button {
    height: 42px;
    font-size: 15px;
    font-weight: 500;
    border-radius: 10px;
    transition: all 0.3s ease;
    margin-bottom: 15px;
    background: linear-gradient(90deg, #409EFF, #79bbff);
    border: none;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
      background: linear-gradient(90deg, #3a8ee6, #66b1ff);
    }

    &:active {
      transform: translateY(0);
    }
  }

  .link-type {
    display: inline-block;
    font-size: 13px;
    color: #409EFF;
    text-decoration: none;
    margin-top: 10px;
    transition: all 0.3s ease;
    position: relative;
    padding-bottom: 2px;

    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 0;
      height: 1px;
      background: #409EFF;
      transition: all 0.3s ease;
    }

    &:hover {
      color: #66b1ff;
      transform: translateX(3px);

      &::after {
        width: 100%;
      }
    }
  }
}

// 响应式设计
@media screen and (max-width: 576px) {
  .register-form {
    width: 85%;
    padding: 25px;
    margin: 15px;

    .title {
      font-size: 22px;
      margin-bottom: 25px;
      
      &::after {
        width: 40px;
      }
    }

    .el-input {
      height: 40px;

      :deep(input) {
        height: 40px;
        font-size: 14px;
      }
    }

    .register-code {
      height: 40px;
    }

    .el-button {
      height: 40px;
      font-size: 14px;
    }
  }
}
</style> 